﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.ViewModels
{
	public class Main : GalaSoft.MvvmLight.ViewModelBase, GongSolutions.Wpf.DragDrop.IDropTarget
	{
		public System.Collections.Generic.IEnumerable<object> ToolBoxItems
		{
			get
			{
				return FineSoft.Models.InitialData.ExtNameSpace.NameSpaces.OfType<object>().Concat(FineSoft.Models.InitialData.ExtNameSpace.Components);
			}
		}

		private FineSoft.Models.Directory _TopDirectory;
		public FineSoft.Models.Directory TopDirectory
		{
			get
			{
				return this._TopDirectory;
			}
			set
			{
				this.RaisePropertyChanging("TopDirectory");
				this.RaisePropertyChanging("TopDirectories");
				this._TopDirectory = value;
				this.RaisePropertyChanged("TopDirectory");
				this.RaisePropertyChanged("TopDirectories");
			}
		}

		public System.Collections.Generic.IEnumerable<FineSoft.Models.Directory> TopDirectories
		{
			get
			{
				if (this._TopDirectory == null)
					return new FineSoft.Models.Directory[0];
				return new[] { this.TopDirectory };
			}
		}

		private FineSoft.Models.ComponentClass _AddComponentByType;
		public FineSoft.Models.ComponentClass AddComponentByType
		{
			get
			{
				return this._AddComponentByType;
			}
			set
			{
				this.RaisePropertyChanging("AddComponentByType");
				if (this._AddComponentByType == value)
				{
					this._AddComponentByType = null;
					this.RaisePropertyChanged("AddComponentByType");
				}
				this._AddComponentByType = value;
				this.RaisePropertyChanged("AddComponentByType");
				//if (value == null)
				//    return;
				//this._AddComponentByType = null;
			}
		}

		private System.Windows.Point _SelectComponentAtPoint;
		public System.Windows.Point SelectComponentAtPoint
		{
			get
			{
				return this._SelectComponentAtPoint;
			}
			set
			{
				this.RaisePropertyChanging("SelectComponentAtPoint");
				this._SelectComponentAtPoint = value;
				this.RaisePropertyChanged("SelectComponentAtPoint");
			}
		}

		private GalaSoft.MvvmLight.Command.RelayCommand<FineSoft.Models.ComponentClass>_ComponentNodeDoubleClick;
		public GalaSoft.MvvmLight.Command.RelayCommand<FineSoft.Models.ComponentClass> ComponentNodeDoubleClick
		{
			get
			{
				if (this._ComponentNodeDoubleClick == null)
					this._ComponentNodeDoubleClick = new GalaSoft.MvvmLight.Command.RelayCommand<Models.ComponentClass>(component =>
					{
						//GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(component);
						this.AddComponentByType = component;
					});
				return this._ComponentNodeDoubleClick;
			}
		}

		private GalaSoft.MvvmLight.Command.RelayCommand<FineSoft.Models.File>_FileNodeDoubleClick;
		public GalaSoft.MvvmLight.Command.RelayCommand<FineSoft.Models.File> FileNodeDoubleClick
		{
			get
			{
				if (this._FileNodeDoubleClick == null)
					this._FileNodeDoubleClick = new GalaSoft.MvvmLight.Command.RelayCommand<Models.File>(file =>
					{
						//GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(component);
						if (file.AssociatedFiles.Length == 0)
							System.Diagnostics.Process.Start(file.Path);
						else this._Pages.Add(new Page(file));
					});
				return this._FileNodeDoubleClick;
			}
		}

		private System.Collections.Generic.List<Page> _Pages;
		public System.Collections.Generic.List<Page> Pages
		{
			get
			{
				if (this._Pages == null)
					this._Pages = new List<Page>();
				return this._Pages;
			}
		}

		#region IDropTarget 成员

		void GongSolutions.Wpf.DragDrop.IDropTarget.DragOver(GongSolutions.Wpf.DragDrop.IDropInfo dropInfo)
		{
			dropInfo.Effects = System.Windows.DragDropEffects.Move;

			System.Windows.Point position = dropInfo.DropPosition;
			position.Y -= 40;
			//GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(position);
			this.SelectComponentAtPoint = position;
		}

		void GongSolutions.Wpf.DragDrop.IDropTarget.Drop(GongSolutions.Wpf.DragDrop.IDropInfo dropInfo)
		{
			//System.Windows.Point position = dropInfo.DropPosition;
			//position.Y -= 40;
			//GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(new DropComponentInfo((FineSoft.Models.ComponentClass)dropInfo.Data, position));
			this.AddComponentByType = (FineSoft.Models.ComponentClass)dropInfo.Data;
		}

		#endregion
	}
}